import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author LKQ
 * @date 2022/2/22 15:16
 * @description 排序后，双指针指向相同的两个数，一左一右，判断个数
 */
public class Solution2 {
    public static void main(String[] args) {
        Solution2 solution2 = new Solution2();
        int[] nums = {1,1,1,3,3,2,2,2};
        solution2.majorityElement(nums);
    }
    public List<Integer> majorityElement(int[] nums) {
        List<Integer> ans = new ArrayList<>();
        Arrays.sort(nums);
        int n = nums.length, left = 0, right = 0;
        while (right < n) {
            while (right < n && nums[right] == nums[left]) {
                right++;
            }
            right--;
            if (right - left + 1  > n / 3) {
                ans.add(nums[left]);
            }
            left = right + 1;
            right = left;
        }
        return ans;
    }
}
